package com.ykl.demo.comp.conv;

import com.ykl.utils.SpringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.AvailabilityState;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.event.EventListener;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2021/11/28 10:21.
 */
public interface AutoRegisterConverter<S,T> extends Converter<S,T>, InitializingBean {

    default void afterPropertiesSet() {
    }

    @EventListener(AvailabilityChangeEvent.class)
    default void onEvent(AvailabilityChangeEvent e){
        if(e.getState()== ReadinessState.ACCEPTING_TRAFFIC){
            GenericConversionService serv = SpringUtils.getBean(GenericConversionService.class);
            if(serv!=null) {
                serv.addConverter(this);
            }
        }
    }

}
